﻿
using System;
using System.Collections.Generic;
using System.Text;

namespace WCYCReader.Drawing
{
    public class EncodingConversion
    {
        public static string bytesToString(sbyte[] source, string charsetToEncodeWith)
        {
            List<byte> bytes = new List<byte>();
            foreach (sbyte sb in source)
            {
                bytes.Add(Convert.ToByte(sb));
            }
            //byte[] bytes = sbytes.Cast<byte>().ToArray();
            if (string.Compare(charsetToEncodeWith, "UTF-8", StringComparison.OrdinalIgnoreCase) == 0)
            {
                //return Encoding.UTF8.GetString((byte[]) source, 0, source.Length);
                return Encoding.UTF8.GetString(bytes.ToArray(), 0, source.Length);
            }
            if ((string.Compare(charsetToEncodeWith, "WINDOWS-1252", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(charsetToEncodeWith, "ISO-8859-1", StringComparison.OrdinalIgnoreCase) != 0))
            {
                throw new UnsupportedEncodingException("EncodingConversion", "charsetToEncodeWith");
            }
            return UnicodeUtils.stringFromEncoding(source, 0, source.Length, 0x4e4);
        }

        public static string bytesToString(sbyte[] source, int index, int count)
        {
            return UnicodeUtils.stringFromWestern(source, index, count);
        }

        public static string charsToString(char[] source)
        {
            return new string(source);
        }

        public static bool equals(string string1, string string2)
        {
            return (string.CompareOrdinal(string1, string2) == 0);
        }

        public static bool equalsIgnoreCase(string string1, string string2)
        {
            return string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);
        }

        public static sbyte[] stringToBytes(string aString)
        {
            return UnicodeWestern.convert(aString);
        }

        public static sbyte[] stringToBytes(string inputString, string charsetToEncodeWith)
        {
            if (string.Compare(charsetToEncodeWith, "UTF-8", StringComparison.OrdinalIgnoreCase) == 0)
            {
                //return (sbyte[])Encoding.UTF8.GetBytes(inputString);
                byte[] bytes = Encoding.UTF8.GetBytes(inputString);
                sbyte[] sbytes = new sbyte[bytes.Length];
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] > 127)
                        sbytes[i] = (sbyte)(bytes[i] - 256);
                    else
                        sbytes[i] = (sbyte)bytes[i];
                }
            }
            if ((string.Compare(charsetToEncodeWith, "WINDOWS-1252", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(charsetToEncodeWith, "ISO-8859-1", StringComparison.OrdinalIgnoreCase) != 0))
            {
                throw new UnsupportedEncodingException("EncodingConversion", "charsetToEncodeWith");
            }
            return UnicodeWestern.convert(inputString);
        }
    }
}


